home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d26 / asmtut4.arc / CHAP24.DOC < prev    next >
Text File  |  1990-08-10  |  8KB  |  217 lines

  1.  
  2.  
  3.  
  4.                                                                            264
  5.  
  6.                         CHAPTER 24 - MISCELLANEOUS INSTRUCTIONS
  7.  
  8.  
  9.              There are a few more assembler instructions which have not been
  10.              covered. Some are seldom used and some are used in special
  11.              circumstances. This chapter gives a brief explanation of them.
  12.  
  13.  
  14.              XCHG
  15.  
  16.              XCHG, the exchange instruction, switches the contents of two
  17.              registers or of a register and a memory location.
  18.  
  19.                  xchg ax, bx
  20.                  xchg al, dl
  21.                  xchg variable1, si
  22.                  xchg ch, variable2
  23.                  xchg [si], ax
  24.                  xchg bh, [di]
  25.  
  26.              It operates on either a word or a byte. It cannot switch two
  27.              memory locations, and it does not operate on the segment
  28.              registers, only on the 8 arithmetic registers. 
  29.  
  30.  
  31.              ESC
  32.  
  33.              The escape instruction is not a complete instruction, it is the
  34.              beginning of an instruction. It signals the 8086 that the first
  35.              two bytes of the instruction contain a co-processor instruction.
  36.              The 8087 is a mathematics co-processor. It reads the instructions
  37.              at the same time as the 8086, and when it sees an escape
  38.              instruction meant for it, it performs that operation. The 8086
  39.              does nothing unless there is a memory address involved. In that
  40.              case the 8086 calculates the address and gives the address to the
  41.              8087. 
  42.  
  43.                  fld  DWORD PTR [si]
  44.                  fmul st, st(3)
  45.                  fisub WORD PTR [di]
  46.  
  47.              are all 8087 instructions that the assembler codes with the
  48.              escape code. You will never code the escape instruction yourself.
  49.  
  50.  
  51.              WAIT (FWAIT)
  52.  
  53.              The 8087 operates independently of the 8086. They can both
  54.              perform operations at the same time, but it is possible for them
  55.              to interfere with each other. If both the 8086 and 8087 are
  56.              reading from or writing to memory, or if one is reading from
  57.              while the other is writing to memory, the read/write will be
  58.              corrupted. In order to stop this, whenever you access memory with
  59.              the 8087, you need to put in a WAIT instruction. WAIT (or FWAIT
  60.  
  61.              ______________________
  62.  
  63.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  64.  
  65.  
  66.  
  67.  
  68.              Chapter 24 - Miscellaneous Instructions                       265
  69.              _______________________________________
  70.  
  71.              which is the same thing), suspends operation of the 8086 until
  72.              the co-processor is finished. It would look like this:
  73.  
  74.                  fstp DWORD PTR [bx]
  75.                  fwait
  76.  
  77.              The 8086 will wait until the 8087 is finished storing into
  78.              memory.
  79.  
  80.              There must also be a wait between 8087 instructions. This is to
  81.              keep the 8087 from starting a second instruction before it is
  82.              finished with the first one. The instruction execution is done by
  83.              the 8087, but the timing is done by the 8086. If you had the
  84.              folllowing code:
  85.  
  86.                  fmul st, st(3)
  87.                  fadd st, st(2)
  88.                  fsub st, st(4)
  89.  
  90.              the 8086 would be past the third instruction before the 8087 had
  91.              time to finish doing the first instruction. The proper coding is:
  92.  
  93.                  fmul st, st(3)
  94.                  fwait
  95.                  fadd st, st(2)
  96.                  fwait
  97.                  fsub st, st(4)
  98.  
  99.              This should concern you only if you program the 8087. It is
  100.              outside the realm of this book. Remember, WAIT and FWAIT are the
  101.              same instruction.
  102.  
  103.  
  104.              LOCK
  105.  
  106.              LOCK is not really of concern to a PC programmer. On some systems
  107.              it is possible to have more than one 8086 that has access to the
  108.              same memory. The problem then arises as it did with the 8087 that
  109.              there is the possibility of two 8086s doing read/write operations
  110.              to memory at the same time. This will result in corrupted data.
  111.              LOCK allows an 8086 to lock out other 8086s. It will be the only
  112.              one allowed to read to or write to memory during the next
  113.              instruction. This is mostly of concern to people who write code
  114.              for peripheral devices which have DMA (direct memory access).
  115.  
  116.  
  117.              LOOPE/LOOPZ  LOOPNE/LOOPNZ
  118.  
  119.              We have used the loop instruction, but these are special cases.
  120.              Remember, the general loop instruction decrements CX by 1, and if
  121.              the result is not zero, it jumps to the top of the loop. In
  122.              special circumstances, you not only want to check the counter in
  123.              CX, but you also want to check the result of some other
  124.              operation.
  125.  
  126.                  test  ax, 5
  127.                  loope outer_loop
  128.  
  129.  
  130.  
  131.  
  132.              The PC Assembler Tutor                                        266
  133.              ______________________
  134.  
  135.  
  136.              will loop if cx is not zero AND ax = 5
  137.  
  138.                  test   ax, 5
  139.                  loopne outer_loop
  140.  
  141.              will loop if cx is not zero AND ax is not 5.
  142.  
  143.  
  144.              HALT
  145.  
  146.              HALT actually halts the operation of the 8086. It can only be
  147.              started again by a reset or an interrupt. If you write:
  148.  
  149.                  cli       ; clear interrupt flag
  150.                  halt
  151.  
  152.              Then normal interrupts can't happen and you have effectively shut
  153.              down the system. One place this might be of use is if you have a
  154.              copy protection scheme and you detect that someone has violated
  155.              it. It halts the system and you need to reset to start again.
  156.              Another place might be if you are writing a game -  someone
  157.              inadvertently enters the "Dungeon of Darkness" and you shut the
  158.              system down.
  159.  
  160.  
  161.              CMC
  162.  
  163.              CMC (complement the carry flag) toggles the carry flag. If it is
  164.              off this turns it on, and if it is on, this turns it off. Why
  165.              this instruction exists is a complete mystery to me. Why would
  166.              you want it as part of the instruction set?
  167.  
  168.  
  169.              LAHF
  170.  
  171.              LAHF (load AH from flags) stores half of the flags in AH. The
  172.              register looks like this:
  173.  
  174.                  7 6 5 4 3 2 1 0
  175.                  S Z   A   P   C
  176.  
  177.              Where these are the Sign, Zero, Auxillary, Parity and Carry
  178.              flags. This is a leftover from earlier Intel chips. All these
  179.              flags are testable so you dont need to look at them, and if you
  180.              want to save them, then:
  181.  
  182.                  pushf
  183.  
  184.              does the trick. Notice that AH does not contain DF, the direction
  185.              flag or IEF, the interrupt enable flag, which are things you
  186.              CAN'T test with a jump instruction. To test for them you need to:
  187.  
  188.                  pushf
  189.                  pop  ax
  190.  
  191.              Then AX contains all the flags.
  192.  
  193.  
  194.  
  195.  
  196.              Chapter 24 - Miscellaneous Instructions                       267
  197.              _______________________________________
  198.  
  199.  
  200.  
  201.              SAHF
  202.  
  203.              SAHF (store AH to flags) is the counterpart to the above
  204.              instruction. It puts AH into the low half of the flags register.
  205.              The comments about LAHF apply to this instruction also.
  206.  
  207.  
  208.  
  209.              NOP
  210.  
  211.              And finally, NOP does absolutely nothing. It is there in case you
  212.              need to fill space, either because you have taken out an
  213.              instruction or for reasons of alignment. It also allows a
  214.              debugger to put the single byte INT (int 3) in the code and then
  215.              replace it with NOP when the breakpoint is no longer desired.
  216.  
  217.